﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XDA_ROM_Hub
{
    public partial class xrh_nandroid_backup : Form
    {
        public xrh_nandroid_backup()
        {
            InitializeComponent();
            
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
        	//remount
            string strCmdText0;
            strCmdText0 = "remount";
            System.Diagnostics.ProcessStartInfo p0 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText0);
            System.Diagnostics.Process proc0 = new System.Diagnostics.Process();
            proc0.StartInfo = p0;
            p0.CreateNoWindow = true;
            proc0.Start();
            proc0.WaitForExit();
            //push script
            string strCmdText;
            strCmdText = "push xrh_backup /system/bin";
            System.Diagnostics.ProcessStartInfo p1 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText);
            System.Diagnostics.Process proc1 = new System.Diagnostics.Process();
            proc1.StartInfo = p1;
            p1.CreateNoWindow = true;
            proc1.Start();
            proc1.WaitForExit();
            //chmod script
            string strCmdText2 = "shell chmod 0777 /system/bin/xrh_backup";
            System.Diagnostics.ProcessStartInfo p2 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText2);
            System.Diagnostics.Process proc2 = new System.Diagnostics.Process();
            proc2.StartInfo = p2;
            p2.CreateNoWindow = true;
            proc2.Start();
            proc2.WaitForExit();
            //execute script
            string strCmdText3;
            if (textBox1.Text == "Backup Name (No spaces allowed)")
            {
                strCmdText3 = "shell xrh_backup";
            }
            else if (textBox1.Text == "")
            {
                strCmdText3 = "shell xrh_backup";
            }
            else if (textBox1.Text.Contains(" "))
            {
                strCmdText3 = "shell xrh_backup";
            }
            else
            {
                strCmdText3 = "shell xrh_backup " + textBox1.Text;
            }
            System.Diagnostics.ProcessStartInfo p3 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText3);
            System.Diagnostics.Process proc3 = new System.Diagnostics.Process();
            proc3.StartInfo = p3;
            proc3.Start();
            proc3.WaitForExit();

            MessageBox.Show("Nandroid Backup completed!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Install script
            string strCmdText0;
            strCmdText0 = "push xdevilium_onandroid_data_backup system/bin";
            System.Diagnostics.ProcessStartInfo p0 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText0);
            System.Diagnostics.Process proc0 = new System.Diagnostics.Process();
            proc0.StartInfo = p0;
            p0.CreateNoWindow = true;
            proc0.Start();
            proc0.WaitForExit();

            //Install script 2
            string strCmdText1;
            strCmdText1 = "push xdevilium_onandroid_backup system/bin";
            System.Diagnostics.ProcessStartInfo p1 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText1);
            System.Diagnostics.Process proc1 = new System.Diagnostics.Process();
            proc1.StartInfo = p1;
            p1.CreateNoWindow = true;
            proc1.Start();
            proc1.WaitForExit();

            //chmod
            string strCmdText2;
            strCmdText2 = "shell chmod 655 system/bin/xdevilium_onandroid_data_backup";
            System.Diagnostics.ProcessStartInfo p2 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText2);
            System.Diagnostics.Process proc2 = new System.Diagnostics.Process();
            proc2.StartInfo = p2;
            p2.CreateNoWindow = true;
            proc2.Start();
            proc2.WaitForExit();

            //chmod
            string strCmdText3;
            strCmdText3 = "shell chmod 655 system/bin/xdevilium_onandroid_backup";
            System.Diagnostics.ProcessStartInfo p3 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText3);
            System.Diagnostics.Process proc3 = new System.Diagnostics.Process();
            proc3.StartInfo = p3;
            p3.CreateNoWindow = true;
            proc3.Start();
            proc3.WaitForExit();

            //Install
            string strCmdText4;
            strCmdText4 = "install backup_app.apk";
            System.Diagnostics.ProcessStartInfo p4 = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + @"\BIN\adb.exe", strCmdText4);
            System.Diagnostics.Process proc4 = new System.Diagnostics.Process();
            proc4.StartInfo = p4;
            p4.CreateNoWindow = true;
            proc4.Start();
            proc4.WaitForExit();

            MessageBox.Show("Application installed.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
    }
}
